package com.ie.service.before;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.mapper.before.CartMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xujia
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartEntity> implements CartService {
    @Override
    public ResponseResult<Map<String, Object>> addCart(CartEntity cartEntity) {
        // 查询购物车里是否有该用户名下的相同商品
        Long count = lambdaQuery()
                .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId())
                .count();
        boolean flag = false;

        if (count == 0) {
            flag = save(cartEntity);

        } else {
            LambdaUpdateWrapper<CartEntity> lambdaupdate = new LambdaUpdateWrapper<>();
            lambdaupdate.set(CartEntity::getShoppingnum, cartEntity.getShoppingnum() + 1)
                    .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                    .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId());
            flag = update(lambdaupdate);
        }
        if (flag) {
            return ResponseResult.getMessageResult(null, "A001");
        } else {
            return ResponseResult.getMessageResult(null, "A002");
        }

    }

    @Override
    public ResponseResult<Map<String, Object>> clearCart(CartEntity cartEntity) {
        try {
            LambdaQueryWrapper<CartEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(CartEntity::getBusertableId, cartEntity.getBusertableId());
            remove(lambdaQueryWrapper);
            return ResponseResult.getMessageResult(null, "A001");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> removeCart(CartEntity cartEntity) {
        try {
            removeById(cartEntity);
            return ResponseResult.getMessageResult(null, "A001");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> bupDateCart(CartEntity cartEntity) {
        List<Integer> bcid = cartEntity.getBcid();
        List<Integer> bshoppingnum = cartEntity.getBshoppingnum();
        ArrayList<CartEntity> bcarts = new ArrayList<>();
        for (int i = 0; i < bcid.size(); i++) {
            CartEntity ce = new CartEntity();
            ce.setId(bcid.get(i));
            ce.setShoppingnum(bshoppingnum.get(i));
            bcarts.add(ce);
        }
        // 批量修改
        boolean flag = updateBatchById(bcarts);
        if (flag) {
            return ResponseResult.getMessageResult(null, "A001"); }
        else {
            return ResponseResult.getMessageResult(null, "A002");
        }


    }
}
